package cn.mdmm.controller.file;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.mdmm.controller.BaseController;
import cn.modoumama.common.utils.MultipartFileUtil;


/**       
 * <p>项目名称：Health_Scale_System<br>
 * 类名称：com.daboo.controller.plan.BalanceUserHealthPlanController<br> 
 * 类描述：健康计划<br>
 *</p>
 * @author 邓强<br>   
 * @date 2017年2月20日上午10:43:28<br>     
 * @version V1.0 <br>     
 */

@Controller
@RequestMapping(value = "/file")
public class FileController extends BaseController {
	Logger log = LoggerFactory.getLogger(getClass());
    
	/**  
	 * <p>上传文件</p> 
	 * @param request
	 * @param balanceUserHealthPlanModel
	 * @return   
	 * @author 邓强
	 * @date 2017年2月20日上午11:44:35
	 */ 
	@RequestMapping(value = "upload")
	@ResponseBody
	public Map<String, Object> upload(HttpServletRequest request,MultipartFile upfile){
		//{"state": "SUCCESS","title": "20170303_1488502564030022030.jpg","original": "joinplan1.jpg","type": ".jpg","url": "/static/upload/file/20170303_1488502564030022030.jpg","size": "38928"}
		Map<String, Object> reMap = new HashMap<>();
		String url = MultipartFileUtil.saveFile(upfile);
		if(url != null){
			reMap.put("state", "SUCCESS");
			reMap.put("url", url);
			reMap.put("type", url.replaceAll(".+(\\.\\w{2,3})$", "$1"));
			reMap.put("size", upfile.getSize());
		}else{
			reMap.put("state", "FAILD");
		}
		return reMap;
	}
}
